package utils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

public class RemakeUtil {

    public static void processWalkImg(String path) throws IOException {
        File file = new File(path+"/resources/image/walkleft");
        if(file.exists()){
                System.out.println("多级目录已经存在不需要创建！！");
        }else{
            file.mkdirs();
        }
        //想命名的原文件夹的路径
        File file1 = new File(path+"/resources/image/walk");
        //将原文件夹更改为A，其中路径是必要的。注意
        file1.renameTo(new File(path+"/resources/image/walkright"));
        List<String> walkList = FileUtil.getFilesName(path+"/resources/image/walkright/", null);
        for (int i = 0; i < walkList.size(); i++) {
            BufferedImage cache = CommonUtil.getFlippedImage(ImageIO.read(new FileInputStream(path+"/resources/image/walkright/walk" + i + ".png")));
            ImageIO.write(cache, "png", new File(path+"/resources/image/walkleft/walk" + i + ".png"));
        }
    }

    public static void processAllWalkImg(String path) throws IOException {
        List<String> skinList = FileUtil.getFilesName(path, null);
        for (String skinPath : skinList){
            processWalkImg(skinPath);
        }
    }

    public static void renameOldResource(String path) {
        List<String> skinList = FileUtil.getFilesName(path, null);
        for (String skinPath : skinList){
            //想命名的原文件夹的路径
            File file1 = new File(skinPath);
            String newPath = skinPath.replace("(", "_").replace(")", "");
            //将原文件夹更改为A，其中路径是必要的。注意
            file1.renameTo(new File(newPath));
        }
    }

    public static void renameImg(String path) throws IOException {
        List<String> cacheList = FileUtil.getFilesName(path+"/resources/image", null);
        for (String cachePath : cacheList){
            String floderName = cachePath.substring(cachePath.lastIndexOf("\\")+1);
            if (floderName.contains("walk")){
                floderName = "walk";
            }
            if (!cachePath.contains("properties")){
                List<String> imgList = FileUtil.getFilesName(cachePath+"/images", null);
                int i = 0;
                for (String imgPath : imgList){
                    File file1 = new File(imgPath);
                    String imgName = imgPath.substring(imgPath.lastIndexOf("_")+1);
                    if (imgName.substring(0,1).equals("0") && imgName.substring(1,2).equals("0")){
                        imgName = floderName + imgName.substring(2);
                    } else if (imgName.substring(0,1).equals("0") && !imgName.substring(1,2).equals("0")){
                        imgName = floderName + imgName.substring(1);
                    }else {
                        imgName = floderName + imgName;
                    }
                    file1.renameTo(new File(cachePath+"/images/"+imgName));
                    if (i != 0){
                        FileUtil.copyFile(new File(cachePath+"/images/"+imgName), cachePath);
                    }
                    System.out.println(file1.getAbsolutePath());
                    i++;
                }
                FileUtil.deleteFile(new File(cachePath+"/images"));
            }
        }
        //想命名的原文件夹的路径
        File file1 = new File(path);
        String newPath = path.replace("(", "_").replace(")", "");
        //将原文件夹更改为A，其中路径是必要的。注意
        file1.renameTo(new File(newPath));
    }

    public static void renameAllImg(String path) throws IOException {
        List<String> skinList = FileUtil.getFilesName(path, null);
        for (String skinPath : skinList){
            renameAllImg(skinPath);
        }
    }

}
